/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012 Oliver Katz */

#include "mod_display_engine.h"

namespace sleepyface
{
    namespace arch
    {
        namespace modules
        {
            namespace display_engine
            {
				int handleInitialize(ModuleSignal s, ArchModule &m, Architecture &a)
				{
					if (s.size() != 1)
						return 1;

					visual::display_engine::DisplayEngineLoader *del = new visual::display_engine::DisplayEngineLoader(s.getData(0).getString());
					if (!del->good())
						return 2;

					visual::display_engine::DisplayEngine *de = new visual::display_engine::DisplayEngine();
					vector<string> widgets = del->listWidgetRenderers();
					for (vector<string>::iterator i = widgets.begin(); i != widgets.end(); i++)
					{
						visual::display_engine::WidgetRenderer wr = del->getWidgetRenderer(*i);
						de->addWidgetRenderer(*i, wr);
					}
					vector<string> containers = del->listContainerRenderers();
					for (vector<string>::iterator i = containers.begin(); i != containers.end(); i++)
					{
						visual::display_engine::ContainerRenderer cr = del->getContainerRenderer(*i);
						de->addContainerRenderer(*i, cr);
					}
					m.buf.set("display-engine-loader", ModuleValue(ModuleValue::_data, del));
					m.buf.set("display-engine", ModuleValue(ModuleValue::_data, de));
					return 0;
				}

				int handleUninitialize(ModuleSignal s, ArchModule &m, Architecture &a)
				{
					delete (visual::display_engine::DisplayEngineLoader *)m.buf.get("display-engine-loader").getData();
					delete (visual::display_engine::DisplayEngine *)m.buf.get("display-engine").getData();
					return 0;
				}

				int handleRender(ModuleSignal s, ArchModule &m, Architecture &a)
				{
					return ((visual::display_engine::DisplayEngine *)m.buf.get("display-engine").getData())->recursivelyRenderContainer(*(visual::layout_engine::Container *)s.getData(0).getData(), 0, 0, visual::display_engine::DisplayBox(s.getData(1).getInt(), s.getData(2).getInt()));
				}

				ArchModule createModule()
				{
					ArchModule rtn = ArchModule();
					rtn.setName("display-engine");
					rtn.bindSignal(ArchModule::signalInitialize, handleInitialize);
					rtn.bindSignal(ArchModule::signalUninitialize, handleUninitialize);
					rtn.bindSignal(signalRender, handleRender);
					return rtn;
				}
			}
		}
	}
}